package com.librarysys.service.impl;

import com.librarysys.common.Rank;
import com.librarysys.common.Result;
import com.librarysys.dao.*;
import com.librarysys.entity.*;
import com.librarysys.service.IOtherService;
import com.librarysys.vo.BookListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuhui on 2017/7/11.
 */
@Service("iOtherService")
public class OtherServiceImpl implements IOtherService {
    @Autowired
    private LibraryMapper libraryMapper;

    @Autowired
    private BorrowMapper borrowMapper;

    @Autowired
    private BookinfoMapper bookinfoMapper;

    @Autowired
    private BookcaseMapper bookcaseMapper;

    @Autowired
    private BooktypeMapper booktypeMapper;

    @Autowired
    private PublishingMapper publishingMapper;
    @Autowired
    private  ParameterMapper parameterMapper;



    @Override
    public Result getLibrary(Integer id) {
        Library library = libraryMapper.selectByPrimaryKey(id);
        return Result.createBySuccess("获取图书馆信息成功", library);
    }

    public Result saveLibrary(Library library) {
        int i = libraryMapper.updateByPrimaryKey(library);
        if(i>0){
            return Result.createBySuccess("更新成功");
        }else{
            return Result.createByError("更新失败");
        }
    }

    public Result index(){
        List<Rank> objects = borrowMapper.selectOrder();
        List<BookListVO> lists=new ArrayList<BookListVO>();
        int rank=1;
        for(Rank objs:objects){
            Bookinfo bookinfo = bookinfoMapper.selectByPrimaryKey(new Integer(objs.getBookid().toString()));
            BookListVO bookListVO=new BookListVO();
            bookListVO.setRank(rank++);
            bookListVO.setAuthor(bookinfo.getAuthor());
            bookListVO.setBarcode(bookinfo.getBarcode());
            Bookcase bookcase = bookcaseMapper.selectByPrimaryKey(bookinfo.getBookcase());
            if(bookcase==null){
                bookListVO.setBookcase("");
            }else{
                bookListVO.setBookcase(bookcase.getName());
            }
            bookListVO.setBookName(bookinfo.getBookname());
            bookListVO.setBorowTimes(new Integer(objs.getS().toString()));
            bookListVO.setPrice(bookinfo.getPrice());
            Booktype booktype = booktypeMapper.selectByPrimaryKey(bookinfo.getTypeid());
            if(booktype==null){
                bookListVO.setTypeNmae("");
            }else{
                bookListVO.setTypeNmae(booktype.getTypename());
            }
            Publishing publishing = publishingMapper.selectBy(bookinfo.getIsbn());
            if(publishing==null){
                bookListVO.setPublishing("");
            }else{
                bookListVO.setPublishing(publishing.getPubname());
            }
            lists.add(bookListVO);
        }
        return Result.createBySuccess("获取排名信息成功",lists);
    }

    public Result<Parameter> getParameter(){
        Parameter parameter = parameterMapper.selectBy();
        return Result.createBySuccess("查询成功",parameter);
    }

    public Result saveParameter(Integer cost,Integer validity){
        int i = parameterMapper.updateByPrimaryKey(new Parameter(1, cost, validity));
        if(i>0){
            return Result.createBySuccess("更新成功");
        }else{
            return Result.createByError("更新失败");
        }
    }

    public Result listBookcase(){
        List<Bookcase> bookcases = bookcaseMapper.selectAll();
        return Result.createBySuccess("获取书架信息成功",bookcases);
    }
}
